Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

schematized

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

schematized

Turn objects into JSON schemas! The more examples you provide, the better your schema will be.

  • 1.9.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Schematized

npm version Build-Test-Publish semantic-release XO code style

Turn objects into JSON schemas! The more examples you provide, the better your schema will be.

A Node port of the Python module GenSON but with more inferred constraints.

Example use case: Generate JSON schemas using your API tests, then use the schemas to validate. To keep up to date, Write a test that compares your current schema with the generated schema. Then when your API changes, just update the tests with the newly generated schemas and move on with your day.



:rocket: Quick start

  1. Add dependency
yarn add schematized
  1. Basic usage : See output
import SchemaBuilder from 'schematized' // Typescript & ESM
const { default: SchemaBuilder } = require('schematized') // CommonJS

const builder = new SchemaBuilder()

// Consume JSON
builder.addObject({
  token: '74aea1a53d68b77e4f1f55fa90a7eb81',
  role: ['Basic'],
})

// Produce JSON Schemas!
const schema = builder.toPrettySchema()
  1. Improve the schema with examples : See output
...

builder.addObject({
  token: 'Bearer 6498d9afc96d1d8d881a2b7ded4f9290',
  role: [
    'Admin',
    'Basic',
    'Publisher'
  ]
})
  1. Improve the schema with existing schemas : See output
...

builder.addSchema({
  title: '/user server response',
  description: '/user server response'
})

Schema from the single example

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "token": {
      "type": "string",
      "maxLength": 32,
      "minLength": 32
    },
    "role": {
      "type": "array",
      "items": {
        "type": "string",
        "maxLength": 5,
        "minLength": 5
      }
    }
  },
  "required": [
    "role",
    "token"
  ],
  "additionalProperties": false,
  "maxProperties": 2,
  "minProperties": 2
}

Schema from the two examples

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "token": {
      "type": "string",
      "maxLength": 39,
      "minLength": 32
    },
    "role": {
      "type": "array",
      "items": {
        "type": "string",
        "maxLength": 9,
        "minLength": 5
      }
    }
  },
  "required": [
    "role",
    "token"
  ],
  "additionalProperties": false,
  "maxProperties": 2,
  "minProperties": 2
}

Schema from the two examples and the schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "/user server response",
  "description": "/user server response",
  "type": "object",
  "properties": {
    "token": {
      "type": "string",
      "maxLength": 39,
      "minLength": 32
    },
    "role": {
      "type": "array",
      "items": {
        "type": "string",
        "maxLength": 9,
        "minLength": 5
      }
    }
  },
  "required": [
    "role",
    "token"
  ],
  "additionalProperties": false,
  "maxProperties": 2,
  "minProperties": 2
}

:books: API

MethodDefinitionParameter
.addObject($object)Add an example to improve the generated schema.Valid JSON object
.addSchema($schema)Add schemas to improve the generated schema.Valid JSON Schema
.toSchema()Generate the schema.None
.toPrettySchema()Generate the schema and pretty print.None

:dart: Supported Schema Features

Visit the official JSON Schema site for specification details.

Types

TypeSupported
StringYes
NumberYes
IntegerNever
ObjectYes
ArrayYes
TupleNot yet
BooleanYes
NullYes

Typeless

ConstraintaddSchema()addObject()
titleYesNever
descriptionYesNever
$commentYesNever
defaultNot yetNot yet
examplesNot yetNot yet
enumNot yetNot yet
constNot yetNot yet
anyOfYesYes
allOfNot yetNot yet
oneOfNot yetNot yet
notNot yetNot yet
if/then/elseNot yetNot yet

String

ConstraintaddSchema()addObject()
maxLengthYesYes
minLengthYesYes
formatYesYes
patternNot yetNot yet
contentMediaTypeNot yetNot yet
contentEncodingNot yetNot yet
Supported String formats
FormatSupported?
date-timeYes
dateYes
timeYes
emailYes
hostnameNot yet
idn-hostnameNot yet
ipv4Yes
ipv6Yes
uriYes
uri-referenceNot yet
urlYes
uuidYes
iriNot yet
iri-referenceNot yet
uri-templateNot yet
json-pointerNot yet
relative-json-pointerNot yet
regexNot yet

Number

ConstraintaddSchema()addObject()
maximumYesYes
minimumYesYes
exclusiveMaximumNot yetNot yet
exclusiveMinimumNot yetNot yet
multipleNot yetNot yet

Object

ConstraintaddSchema()addObject()
propertyPatternsYesYes
additionalPropertiesYesYes
requiredYesYes
maxPropertiesYesYes
minPropertiesYesYes

Array

ConstraintaddSchema()addObject()
maxItemsNot yetNot yet
minItemsNot yetNot yet
uniqueItemsNot yetNot yet

Keywords

FAQs

Package last updated on 01 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc